package mya_dc.shared_classes.gui;

import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Panel;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import java.awt.Container;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.plaf.metal.MetalIconFactory;

import mya_dc.shared_classes.SharedDefinitions;

import com.swtdesigner.SwingResourceManager;

/**
 * Shared GUI components
 * 
 * @author MYA
 *
 */
public class SharedComponents
{
	// messages types of error pop-up box
	public enum EMessageBoxType {
		ERROR_CONNECTIONLOST,
		ERROR_CONNECTIONREFUSED,
		ERROR_CANTLISTEN,
		ERROR_CANTLISTENPORTS,
		ERROR_ILLEGALEPORTS,
		ERROR_ILLEGALEHOSTNAME,
		ERROR_DISCONNECTBEFOREEXIT,
		ERROR_CONNECTION_CREATION_FAILED,
		ERROR_ID_FILE_CORRUPT,
		ERROR_MAINSERVER_UNREACHABLE,
		ERROR_UNKNOWN,
		ERROR_USER_CONNECTED,
		ERROR_DISPLAYING_DATABASE_CONSOLE
	}

	/**
	 * Initialize a pop-up message-box
	 */
	public static void MessageBox(JFrame frame, EMessageBoxType type, int iErrorType)
	{
		String msg = "";
		switch(type)
		{
			case ERROR_CONNECTIONLOST: 		         msg = "Connection to main server was terminated!"; break;
			case ERROR_CONNECTIONREFUSED: 	         msg = "Can't connect to main server!"; break;
			case ERROR_CANTLISTEN: 			         msg = "Can't listen on port. Please open port or change it"; break;
			case ERROR_CANTLISTENPORTS: 		     msg = "Can't listen on one or more of the ports. Please open relevant port(s) or change it"; break;
			case ERROR_ILLEGALEHOSTNAME:	         msg = "Host name or IP address is invalid or unaccessable!"; break;
			case ERROR_ILLEGALEPORTS: 			     msg = "Port number(s) is (are) invalid!"; break;
			case ERROR_CONNECTION_CREATION_FAILED:   msg = "Cannot create connection at this time.\nPlease try again later."; break;
			case ERROR_ID_FILE_CORRUPT: 		     msg = "Critical error occured upon main server registration.\nPlease delete ServerID file and reconnect."; break;
			case ERROR_MAINSERVER_UNREACHABLE: 	     msg = "Main server is unreachable at this time.\nPlease try again later."; break;
			case ERROR_UNKNOWN: 				     msg = "An unknown error occured. Please reattempt the operation."; break;
			case ERROR_USER_CONNECTED:			     msg = "The selected project is active. Please await user disconnection before attempting deletion."; break;
			case ERROR_DISPLAYING_DATABASE_CONSOLE:	 msg = "Console could not be opened. Make sure that h2.jar appears under \"H2\\bin\" in the database root folder.";break;
		}
		JOptionPane.showMessageDialog(frame, msg, "Error", iErrorType);
	}
	
	/**
	 * Generates an icon to be displayed in system tray
	 * 
	 * @return icon for the system tray
	 * @throws HeadlessException
	 */
	public static Image getImage(SharedDefinitions.Entity entity) throws HeadlessException
	{
		String path = "images/icon";
		switch (entity)
		{
			case ENTITY_MASTER: path += "Master.gif"; break;
			case ENTITY_SLAVE: path += "Slave.gif"; break; 
			case ENTITY_COMPILATION: path += "Compilation.gif"; break;
		}
		
		Icon defaultIcon;
		if (!new File(path).isFile()) {
			defaultIcon = MetalIconFactory.getTreeComputerIcon();
		}
		else {
			defaultIcon = SwingResourceManager.getIcon(path);
		}
        Image img = new BufferedImage(
        		defaultIcon.getIconWidth(),
        		defaultIcon.getIconHeight(),
                BufferedImage.TYPE_4BYTE_ABGR);
        defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);
        return img;
    }
	
	/**
	 * Opens a JFileChooser dialog for the user and shows a specified extension.
	 *
	 * @param buttonString - The string to show as the OK button in the dialog.
	 * @param owner		   - The owner of the dialog
	 * @param extension    - The file extension to show
	 * @param description  - Description for the given extension
	 * 
	 * @return A string that the user choose, or null if no path was chosen.
	 */
	public static String getFileWithExtension(
			String buttonString,
			Container owner,
			String extension,
			String description) 
	{
		FileNameExtensionFilter filter = new FileNameExtensionFilter(description, extension);
		JFileChooser fc = new JFileChooser();
		fc.setApproveButtonText(buttonString);
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fc.setFileFilter(filter);
		if (fc.showOpenDialog(owner)==JFileChooser.APPROVE_OPTION)
			return fc.getSelectedFile().getAbsolutePath();
		return null;
	}
	
	/**
	 * Opens a JFileChooser dialog for the user.
	 *
	 * @param type		   - Type of files to show (files/directories... see JFileChooser)
	 * @param owner		   - The owner of the dialog
	 * 
	 * @return A string that the user choose, or "" if no path was chosen.
	 */
	public static String getLocation(int type, Container owner)
	{
		
		JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(type);
		fc.setAcceptAllFileFilterUsed(false);
		if (fc.showOpenDialog(owner)==JFileChooser.APPROVE_OPTION)
		{
				return fc.getSelectedFile().getAbsolutePath();
		}
		return "";
	}
}
